<?php
include_once 'MY/extra_sql.php';
	function GetID($prefix) {
		//第一步:初始化种子
		//microtime(); 是个数组
		$seedstr =split(" ",microtime(),5);
		$seed =$seedstr[0]*10000;
		//第二步:使用种子初始化随机数发生器
		srand($seed);
		//第三步:生成指定范围内的随机数
		$random =rand(1000,10000);
	
		$filename = date("Ymd", time()).$random.$prefix;
	
		return $filename;
	}

class MaterialController extends Zend_Controller_Action{
  
	function init()
	 {
        if($_SESSION['user']==""){
              echo "<script>window.location='../user/login';</script>"; exit;
         }
		$this->registry = Zend_Registry::getInstance();
		$this->view = $this->registry['view'];
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$this->view->template = "material";
		$this->view->conf = new Zend_Config(require './application/configs/config.php');

		/*$group_id = $_SESSION['user']['group_id'];
		$group = new Group();
		$group = $group->getgroupByid($group_id);
		$groups = explode(',',$group[0]['models']);
		if(!in_array('material',$groups)){
			  echo "<script>alert('您没有权限访问此模块');history.go('-1');</script>";
		}*/
	}
	
	
//教材首页
    public function indexAction(){
    	
            $filter=new Zend_Filter_StripTags();
            $material = new Material();
            $page=$filter->filter(($this->_request->getParam('page')));
            $search_name=$filter->filter(($this->_request->getParam('search_name')));
            if($page==""){
                $page="1";
            }
            $materials = $material->getAllMaterials('','',$search_name);
            $total = count($materials);
            $perpage = "10";
            $pages = ceil($total/$perpage);
            
            $this->view->page = $page;
            $this->view->pages = $pages;
            $this->view->total = $total;
            $this->view->materials = $material->getAllMaterials($page,$perpage,$search_name);
            echo $this->view->render('material/index.html');
    }
 
//添加教材方法	
    public function addAction(){
        	
            $filter=new Zend_Filter_StripTags();
            $material = new Material();
            $material_file = new MaterialFile();
            $material_pratice = new MaterialPratice();
            $operation = new Operation();
            if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
                $name=$filter->filter(($this->_request->getPost('name')));
                $duty=$filter->filter(($this->_request->getPost('duty')));
                $start_time=$filter->filter(($this->_request->getPost('start_time')));
                $summary=$filter->filter($this->_request->getPost('summary'));
                $start_time = strtotime(trim($start_time));
                $materials_comment=$filter->filter(($this->_request->getPost('materials_comment')));
                $pratices_comment=$filter->filter(($this->_request->getPost('pratices_comment')));
                $data = array(
                    'name'=>$name,
                    'duty'=>$duty,
                    'start_time'=>$start_time,
                    'summary'=>$summary,  
                    'create_time'=>time()
                );
               //插入教材表      
                $material_id = $material->insert($data);
                
                $path= "public/files/material/".date("Ym",time())."/";//上传路径
		        if(!file_exists($path))
                {
                   mkdir("$path", 0700);
                 }//END IF
                $path= "public/files/material/".date("Ym",time())."/".date("d",time())."/";//上传路径
                if(!file_exists($path))
                {
                  mkdir("$path", 0700);
                 }
               //上传课件附件  
                 $materials_name=$_FILES["materials"]["name"];
                 $file1=GetID( ".".strtolower(substr($_FILES['materials']['name'], (strrpos($_FILES['materials']['name'], '.') + 1))));
                 $materials_path = $path.$file1;
                 move_uploaded_file($_FILES["materials"]["tmp_name"],$materials_path);

                 //上传练习附件  
                 $pratices_name=$_FILES["pratices"]["name"];
                 $file1=GetID( ".".strtolower(substr($_FILES['pratices']['name'], (strrpos($_FILES['pratices']['name'], '.') + 1))));
                 $pratices_path = $path.$file1;
                 move_uploaded_file($_FILES["pratices"]["tmp_name"],$pratices_path);
                  
                 $material_array = array(
                                    'material_id'=>$material_id,
                                    'materials_name'=>$materials_name,
                                    'materials_comment'=>$materials_comment,
                                    'materials_path'=>$materials_path,
                                    'create_time'=>time()
                                    ); 
                $mf_id = $material_file->insert($material_array);                    
                
                $pratice_array = array(
                                    'material_id'=>$material_id,
                                    'pratices_name'=>$pratices_name,
                                    'pratices_comment'=>$pratices_comment,
                                    'pratices_path'=>$pratices_path,
                                    'create_time'=>time()
                                    );    
                $mp_id = $material_pratice->insert($pratice_array);                                 
                if($mf_id){
                    $time = time();
                    $data = array(
                        'op_name'=>'add',
                        'model_name'=>'material',
                        'user_id'=>$_SESSION['user']['user_id'],
                        'user_name'=>$_SESSION['user']['user_name'],
                        'name'=>'增加教材',
                        'create_time'=>$time
                    );
                    $query = $operation->insert($data);
                    echo "<script>window.location='detail?material_id=".$material_id."'</script>";
                }else{
                     
                }
            }
            echo $this->view->render('material/add.html');
        }
   
   //修改教材
     function editAction(){
     	
         	$filter=new Zend_Filter_StripTags();
         	$material = new Material();
         	$materialfile = new MaterialFile();
         	$materialpratice = new MaterialPratice();
            $material_id = $filter->filter($this->_request->getParam('material_id'));
            if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
            	
                $name=$filter->filter(($this->_request->getPost('name')));
                $duty=$filter->filter(($this->_request->getPost('duty')));
                $start_time=$filter->filter(($this->_request->getPost('start_time')));
                $current_materials=$filter->filter(($this->_request->getPost('current_materials')));
                $current_pratices=$filter->filter(($this->_request->getPost('current_pratices')));
                $mf_id=$filter->filter(($this->_request->getPost('mf_id')));
                $mp_id=$filter->filter(($this->_request->getPost('mp_id')));
                $summary=$filter->filter($this->_request->getPost('summary'));
                $start_time = strtotime(trim($start_time));
                $materials_comment=$filter->filter(($this->_request->getPost('materials_comment')));
                $pratices_comment=$filter->filter(($this->_request->getPost('pratices_comment')));
               //更新教材 
                $data = array(
                    'name'=>$name,
                    'duty'=>$duty,
                    'start_time'=>$start_time,
                    'summary'=>$summary,     
                    'create_time'=>time()
                );
                $material->updatematerialBymaterial_id($data,$material_id);
                
                $path= "public/files/material/".date("Ym",time())."/";//上传路径
		        if(!file_exists($path))
                {
                   mkdir("$path", 0700);
                 }//END IF
                $path= "public/files/material/".date("Ym",time())."/".date("d",time())."/";//上传路径
                if(!file_exists($path))
                {
                  mkdir("$path", 0700);
                 }  
             //更新课件附件      
                if($_FILES['materials']['name']!=""){
                 $materials_name=$_FILES["materials"]["name"];
                 $file1=GetID( ".".strtolower(substr($_FILES['materials']['name'], (strrpos($_FILES['materials']['name'], '.') + 1))));
                 $materials_path = $path.$file1;
                 move_uploaded_file($_FILES["materials"]["tmp_name"],$materials_path);
                 
                 $material_array = array(
                                    'material_id'=>$material_id,
                                    'materials_name'=>$materials_name,
                                    'materials_comment'=>$materials_comment,
                                    'materials_path'=>$materials_path,
                                    'create_time'=>time()
                                    ); 
                 $materialfile->updatefilesBymf_id($material_array,$mf_id);                   
                }
             //更新练习附件  
               if($_FILES['pratices']['name']!=""){
                 $pratices_name=$_FILES["pratices"]["name"];
                 $file1=GetID( ".".strtolower(substr($_FILES['pratices']['name'], (strrpos($_FILES['pratices']['name'], '.') + 1))));
                 $pratices_path = $path.$file1;
                 move_uploaded_file($_FILES["pratices"]["tmp_name"],$pratices_path);
                 
                 $pratice_array = array(
                                    'material_id'=>$material_id,
                                    'pratices_name'=>$pratices_name,
                                    'pratices_path'=>$pratices_path,
                                    'pratices_comment'=>$pratices_comment,
                                    'create_time'=>time()
                                    ); 
                 $materialpratice->updatepraticesBymp_id($pratice_array,$mp_id);                   
                }
                
                echo "<script>window.location='detail?material_id=".$material_id."'</script>";
            }else{
	            $materialfiles = $materialfile->getfilesBymaterial_id($material_id);
	         	$materialpratices = $materialpratice->getpraticesBymaterial_id($material_id);
	            $this->view->material = $material->getmaterialBymaterial_id($material_id);
	            
	            $this->view->materialfiles = $materialfiles;
	         	$this->view->materialpratices = $materialpratices;
            }
            echo $this->view->render('material/edit.html');
     }     
   //查看教材详情
     function detailAction(){
     	
         	$filter=new Zend_Filter_StripTags();
         	$material = new Material();
         	$materialfile = new MaterialFile();
         	$materialpratice = new MaterialPratice();
         	$db = new mysql(constant('dbhost'),constant('dbuser'),constant('dbpass'),constant('dbname'),"UTF8");
         	$material_id = $filter->filter($this->_request->getParam('material_id'));
     		$page=$filter->filter(($this->_request->getParam('page')));
			//comment的页数
				if($page==""){
					$page="1";
				}
         	
         	$materials = $material->getmaterialBymaterial_id($material_id);
         	$materialfiles = $materialfile->getfilesBymaterial_id($material_id);
         	$materialpratices = $materialpratice->getpraticesBymaterial_id($material_id);
         	
         	$this->view->materials = $materials;
         	$this->view->materialfiles = $materialfiles;
         	$this->view->materialpratices = $materialpratices;
	      	//查询所有的comment和发表comment的用户名
			$count = $db->select_comment('','',$material_id,'8');
		
		//总条数
			$total = count($count);
		//总页数
			$perpage = "2";
			$pages = ceil($total/$perpage);
			$start = ($page-1)*$perpage;
			$end = $perpage;
			$this->view->page = $page;
			$this->view->pages = $pages;
			$this->view->total = $total;
		
			$this->view->comments = $db->select_comment($start,$end,$material_id,'8');  
			
         	echo $this->view->render('material/detail.html');
         	
     }    

     public function deleteAction(){
     	
            $filter=new Zend_Filter_StripTags();
          	$material = new Material();
         	$materialfile = new MaterialFile();
         	$materialpratice = new MaterialPratice();
         	$material_id = $filter->filter($this->_request->getParam('material_id'));
         	
         	$material->delmaterialBymaterial_id($material_id);
         	$materialfile->delmaterialsBymaterial_id($material_id);
         	$materialpratice->delpraticeBymaterial_id($material_id);
         	echo "<script>window.location='index'</script>";
     }
     
     public function mfdeleteAction(){
     	    $filter=new Zend_Filter_StripTags();
            $materialfile = new MaterialFile();
            $mf_id = $filter->filter($this->_request->getParam('mf_id'));
            $mid = $filter->filter($this->_request->getParam('mid'));
            $materialfile->delmaterialsBymf_id($mf_id);
            echo "<script>window.location='detail?material_id=$mid'</script>";     
     }
     
    public function mpdeleteAction(){
     	    $filter=new Zend_Filter_StripTags();
            $materialpratice = new MaterialPratice();
            $mp_id = $filter->filter($this->_request->getParam('mp_id'));
            $mid = $filter->filter($this->_request->getParam('mid'));
            $materialpratice->delmaterialsBymp_id($mp_id);
            echo "<script>window.location='detail?material_id=$mid'</script>";     
     }
     
}